﻿using SmartJxtzn.Core.Service;
using Microsoft.AspNetCore.Http;
using SmartJxtzn.Application.Entity;
using SqlSugar;
namespace SmartJxtzn.Application;

/// <summary>
/// 测试1服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class Test1Service : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Test1> _test1Rep;
    private ISqlSugarClient _sqlSugarClient;
    public Test1Service(SqlSugarRepository<Test1> test1Rep, ISqlSugarClient sqlSugarClient)
    {
        _test1Rep = test1Rep;
        _sqlSugarClient = sqlSugarClient;
    }

    /// <summary>
    /// 分页查询测试1
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    [DisplayName("分页查询测试1")]
    public async Task<SqlSugarPagedList<Test1Output>> Page(PageTest1Input input)
    {
        input.SearchKey = input.SearchKey?.Trim();
        var query = _test1Rep.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(input.SearchKey), u =>
                u.desc.Contains(input.SearchKey)
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.desc), u => u.desc.Contains(input.desc.Trim()))
            .Select<Test1Output>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加测试1
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    [DisplayName("增加测试1")]
    public async Task<long> Add(AddTest1Input input)
    {
        var entity = input.Adapt<Test1>();
        await _test1Rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除测试1
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    [DisplayName("删除测试1")]
    public async Task Delete(DeleteTest1Input input)
    {
        var entity = await _test1Rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _test1Rep.FakeDeleteAsync(entity);   //假删除
        //await _test1Rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新测试1
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    [DisplayName("更新测试1")]
    public async Task Update(UpdateTest1Input input)
    {
        var entity = input.Adapt<Test1>();
        await _test1Rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取测试1
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    [DisplayName("获取测试1")]
    public async Task<Test1> Detail([FromQuery] QueryByIdTest1Input input)
    {
        return await _test1Rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取测试1列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    [DisplayName("获取测试1列表")]
    public async Task<List<Test1Output>> List([FromQuery] PageTest1Input input)
    {
        return await _test1Rep.AsQueryable().Select<Test1Output>().ToListAsync();
    }

}
